const Sequelize = require('sequelize')
let db = require('../config/db')
let User = require('./user')

let Task = db.define('task',{
    task_id:{
        type: Sequelize.INTEGER,
        field: 'task_id',
        primaryKey:true,
        autoIncrement: true,
        comment: "主键，自增"
    },
    type:{
        type:Sequelize.STRING,
        comment:'任务类型',
        allowNull:false,
        validate:{
            notEmpty: true
        }
    },
    tag:{
        type:Sequelize.STRING,
        comment:'任务标签',
        allowNull:false,
        validate:{
            notEmpty: true
        }
    },
    belong:{
        type:Sequelize.INTEGER,
        defaultValue:null,
        comment:'接任务的人'
    },
    start_time: {
        type: Sequelize.DATE,
        get() {
            return moment(this.getDataValue('start_time')).format('YYYY-MM-DD HH:mm:ss');
        }
    },
    end_time: {
        type: Sequelize.DATE,
        get() {
            return moment(this.getDataValue('end_time')).format('YYYY-MM-DD HH:mm:ss');
        }
    },
    task_title:{
        type:Sequelize.STRING,
        allowNull:false,
        validate:{
            notEmpty: true
        }
    },
    links:{
        type:Sequelize.STRING,
        comment:'任务链接'
    },
    task_addr:{
        type:Sequelize.STRING,
        comment:'任务地址'
    },
    task_addr2:{
        type:Sequelize.STRING,
        comment:'详细地址'
    },
    task_des:{
        type:Sequelize.STRING,
        comment:'任务描述',
        allowNull:false,
        validate:{
            notEmpty: true
        }
    },
    task_require:{
        type:Sequelize.STRING,
        comment:'要求',
    },
    task_con:{
        type:Sequelize.STRING,
        comment:'任务内容',
    },
    task_remarks:{
        type:Sequelize.STRING,
        comment:'任务备注'
    },
    task_cycle:{
        type:Sequelize.STRING,
        comment:'任务周期'
    },
    task_price:{
        type:Sequelize.INTEGER,
        defaultValue:0,
        comment:'任务价格',
        allowNull:false,
        validate:{
            notEmpty: true,
            min:1,
            isNumeric: true,
        }
    },
    task_cash:{
        type:Sequelize.INTEGER,
        defaultValue:0,
        comment:'实际需要支付的价格'
    },
    task_pay:{
        type:Sequelize.INTEGER,
        defaultValue:0,
        comment:'实际支付的价格'
    },
    company_name: {
        type: Sequelize.STRING,
        field: 'company_name',
        comment:'企业名称'
    },
    headimgurl:{
        type:Sequelize.STRING,
        comment:"headimgurl",
        defaultValue: null

    },
    username: {
        type: Sequelize.STRING,
        field: 'username',
        comment:'用户名'
    },
    nickname:{
        type:Sequelize.STRING,
        comment:"用户昵称",
        defaultValue: null
    },
    isPay:{
        type: Sequelize.INTEGER,
        defaultValue:0,
        comment:'状态，是否已支付'
    },
    status:{
        type: Sequelize.INTEGER,
        defaultValue:0,
        comment:'任务状态'
                    /*任务状态
                    0 待审核
                    1 审核通过
                    2 审核不通过
                    3 报名中
                    4 进行中
                    5 乙方完成
                    6 甲方完成
                    */
    },
    deletedAt: {
        type: Sequelize.DATE,
        get() {
            return moment(this.getDataValue('deletedAt')).format('YYYY-MM-DD HH:mm:ss');
        }
    },
    createdAt: {
        type: Sequelize.DATE,
        get() {
            return moment(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm:ss');
        }
    },
    updatedAt: {
        type: Sequelize.DATE,
        get() {
            return moment(this.getDataValue('updatedAt')).format('YYYY-MM-DD HH:mm:ss');
        }
    },
    isDel:{
        type: Sequelize.INTEGER,
        defaultValue:0,
        comment:'状态，是否删除'
    }
},{
    freezeTableName:true,
    paranoid:true,
    timestamps:true
})
User.hasMany(Task, {foreignKey: 'uid', sourceKey: 'uid'});

Task.belongsTo(User, {foreignKey: 'uid', targetKey: 'uid'});
Task.belongsTo(User, {foreignKey: 'belong', targetKey: 'uid'});

let task = Task.sync();
module.exports = Task;
